<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="流程实例ID">
              <a-input v-model="queryParam.deploymentId" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :md="(!advanced && 6) || 24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
            >
              <a-button type="primary" @click="searchQuery">查询</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table ref="table" size="default" rowKey="processDefinitionId" :scroll="{x:true}" :columns="columns" :data="loadDataList">
      <span slot="action" slot-scope="text, record">
        <a @click="getProcessImage(record)">流程图</a>
        <a @click="handleSet(record)" style="margin-left:10px">配置</a>
        <!-- <a-popconfirm style="margin-left:10px" title="确定删除?" @confirm="() => handleDelete(record.id)">
          <a>删除</a>
        </a-popconfirm> -->
      </span>
    </s-table>

    <!-- 设置弹窗 -->
    <ProcessSetModal ref="ProcessSetModal" ></ProcessSetModal>
    <!-- 图片预览 -->
    <a-modal :visible="previewVisible" :width="600" :footer="null" @cancel="previewVisible = false">
      <img alt="example" style="width: 100%" :src="previewImage" />
    </a-modal>

  </a-card>
</template>

<script>
import STable from '@/components/table/'
import XSelectUserByDep from '@/components/extends/XSelectUserByDep'
import XImport from '@/components/extends/XImport.vue'
import ATextarea from 'ant-design-vue/es/input/TextArea'
import AInput from 'ant-design-vue/es/input/Input'
import moment from 'moment'
import { getMaterials ,setMaterial,delMaterial,delMaterials} from '@/api/api'
import {getAction,postAction, deleteAction } from '@/api/manage'
import { axios } from '@/utils/request'
import ProcessSetModal from './modules/ProcessSetModal'

export default {
  name: 'ProcessManage',
  components: {
    AInput,
    ATextarea,
    STable,
    XImport,
    ProcessSetModal,
    XSelectUserByDep
  },
  data() {
    return {
      visibleImport:false,
      toggleSearchStatus:false,
      previewVisible:false,
      previewImage:'',
      form: null,
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        actStatus:10
      },
      url:{
        list:'/workflow/definitionList',
        add:'',
        edit:'',
        exportXlsUrl:'',
        deleteBatch:'/org/society/deleteBatch',
      },
      // 表头
      columns: [
        {
          title: '流程ID',
          align: 'center',
          dataIndex: 'processDefinitionId'
        },
        {
          title: '流程实例ID',
          align: 'center',
          dataIndex: 'deploymentId'
        },
        {
          title: '流程定义名称',
          align: 'center',
          dataIndex: 'processDefinitionName'
        },
        {
          title: '流程定义key',
          align: 'center',
          dataIndex: 'processDefinitionKey'
        },
        {
          title: '流程版本',
          align: 'center',
          dataIndex: 'processDefinitionVersion'
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'suspended',
          customRender:(text,recorde)=>{
            return text?'已挂起':'未挂起'
          }
        },
        {
          title: '操作',
          align: 'center',
          fixed:"right",
          width: '120px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadDataList: parameter => {
        return getAction(this.url.list,Object.assign(parameter, this.queryParam)).then(res => {
          if(res.success&&res.result){
            const obj = {
              data: res.result.records||[],
              pageNo: res.result.current,
              pageSize: res.result.size,
              totalCount: res.result.total,
              totalPage: res.result.page,
              sortColumns: res.result.sortColumns
            }
            return obj
          }else{
            return {}
          }
        })
      },

      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    getProcessImage({processDefinitionKey}){
      if(!processDefinitionKey){
        return this.$message.warning('缺少key')
      }
      axios.get('/workflow/showProcessImageByProcessDefineKey',{
        params:{
          processDefineKey:processDefinitionKey
        },
        responseType:'blob'
      }).then(data=>{
        this.previewImage = URL.createObjectURL(data);
        this.previewVisible = true;
      })
    },
    handleAdd(){
      this.$refs.ProcessModal.title = '添加'
      this.$refs.ProcessModal.add();
    },
    handleEdit(record){
      this.$refs.ProcessModal.title = '编辑'
      this.$refs.ProcessModal.edit(record);
    },
    handleSet(record){
      this.$refs.ProcessSetModal.open(record);
    },
    handleReview(record){
      this.$refs.ProcessModal.review(record);
    },
    handleToggleSearch(){
      this.toggleSearchStatus = !this.toggleSearchStatus;
    },
    // 申请使用
    handleApply(record){
      this.$refs.MaterialApplyModal.title = '场地和物资申请'
      this.$refs.MaterialApplyModal.create({
        materialId:record.id,
        materialName:record.name,
        code:record.code,
        type:record.type,
        applierId: this.$store.getters.userInfo.id,
        realname: this.$store.getters.userInfo.realname,
      });
    },
    handleDelete(id){
      delMaterial({id}).then(res =>{
          if(res.success){
            this.$message.success('删除成功');
            this.searchQuery()
          }else{
            this.$message.error(res.message)
          }
      })
    },
    batchDel: function () {
      if(!this.url.deleteBatch){
        this.$message.error("请设置api.leteBatch属性!")
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return;
      } else {
        var ids = "";
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ",";
        }
        var that = this;
        this.$confirm({
          title: "确认删除",
          content: "是否删除选中数据?",
          onOk: function () {
            that.loading = true;
            deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.selectedRowKeys = [];
                that.selectionRows = [];
                that.searchQuery()
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.loading = false;
            });
          }
        });
      }
    },
    searchQuery() {
      this.$nextTick(() => {
        this.$refs.table.refresh()
      })
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
    },
    onChange(row) {
      this.selectedRowKeys = row.selectedRowKeys
      this.selectedRows = row.selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
